import os
import cv2
from tqdm import tqdm
import glob
import numpy as np


dir1 = r'D:\lliujian\other\新建文件夹\测试照片\测试照片_crop'
dir2 = r'D:\lliujian\other\新建文件夹\测试照片_crop_stitch'
save_dir = os.path.join(dir1, 'diff')
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

for i, img1_path in enumerate(glob.glob(dir1 + '\\*.bmp')):
    img_name = os.path.basename(img1_path)
    # img1 = cv2.imread(img1_path)
    img1 = cv2.imdecode(np.fromfile(img1_path, dtype=np.uint8), -1)
    img2 = cv2.imdecode(np.fromfile(os.path.join(dir2, img_name), dtype=np.uint8), -1)
    # img2 = cv2.imread(os.path.join(dir2, img_name))
    diff = cv2.subtract(img1, img2)
    cv2.imencode('.bmp', diff)[1].tofile(os.path.join(save_dir, img_name))